package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fundaccount.pojo.dto.FundAccountAddDTO;
import com.xbongbong.pro.fundaccount.pojo.dto.FundAccountBatchUpdateDTO;
import com.xbongbong.pro.fundaccount.pojo.dto.FundAccountDeleteDTO;
import com.xbongbong.pro.fundaccount.pojo.dto.FundAccountGetDTO;
import com.xbongbong.pro.fundaccount.pojo.dto.FundAccountStatusDTO;
import com.xbongbong.pro.fundaccount.pojo.vo.FundAccountAddVO;
import com.xbongbong.pro.fundaccount.pojo.vo.FundAccountGetVO;
import com.xbongbong.pro.fundaccount.pojo.vo.FundAccountStatusVO;
import com.xbongbong.saas.service.FundAccountService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 资金账户相关接口
 * @author zhouwq
 * @date 2019/10/23 10:29
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/fund/account")
public class FundAccountController {

    @Resource
    private FundAccountService fundAccountService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 新增资金账户
     * @param fundAccountAddDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid FundAccountAddDTO fundAccountAddDTO, BindingResult br) {
        XbbResponse<FundAccountAddVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(fundAccountAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(fundAccountAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                FundAccountAddVO fundAccountAddVO = fundAccountService.add(fundAccountAddDTO);
                response = new XbbResponse<>(fundAccountAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 更新资金账户
     * @param fundAccountAddDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid FundAccountAddDTO fundAccountAddDTO, BindingResult br) {
        XbbResponse<FundAccountAddVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundAccountAddVO fundAccountAddVO = fundAccountService.update(fundAccountAddDTO);
                response = new XbbResponse<>(fundAccountAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 删除资金账户
     * @param fundAccountDeleteDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid FundAccountDeleteDTO fundAccountDeleteDTO, BindingResult br) {
        XbbResponse<FundAccountAddVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(fundAccountDeleteDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(fundAccountDeleteDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                FundAccountAddVO fundAccountAddVO = fundAccountService.delete(fundAccountDeleteDTO);
                response = new XbbResponse<>(fundAccountAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 启用或禁用资金账户
     * @param fundAccountStatusDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/changeEnableStatus", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeEnableStatus(@RequestBody @Valid FundAccountStatusDTO fundAccountStatusDTO, BindingResult br) {
        XbbResponse<FundAccountStatusVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundAccountStatusVO fundAccountStatusVO = fundAccountService.changeEnableStatus(fundAccountStatusDTO);
                response = new XbbResponse<>(fundAccountStatusVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取所有帐号列表信息
     * @param fundAccountGetDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/get", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid FundAccountGetDTO fundAccountGetDTO, BindingResult br) {
        XbbResponse<FundAccountGetVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundAccountGetVO fundAccountGetVO = fundAccountService.getFundAccounts(fundAccountGetDTO);
                response = new XbbResponse<>(fundAccountGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 更新默认账户
     * @param fundAccountBatchUpdateDTO 入参
     * @param br 校验信息
     * @return json
     */
    @PostMapping(value = "/updateDefaultAccount", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateDefaultAccount(@RequestBody @Valid FundAccountBatchUpdateDTO fundAccountBatchUpdateDTO, BindingResult br) {
        XbbResponse<FundAccountAddVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FundAccountAddVO fundAccountAddVO = fundAccountService.updateDefaultAccount(fundAccountBatchUpdateDTO);
                response = new XbbResponse<>(fundAccountAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
